package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.StudentUserDao;
import dao.UserDao;
import model.User;
import services.UserServiceForStudentManagement;

@WebServlet(value="/StudentRegisterServlet")
public class StudentRegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");   //可以放在过滤器里面
		String username = request.getParameter("username");  //获取用户名
		String password = request.getParameter("password");
		String level = request.getParameter("level");
		User user=new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setLevel(level);
		if(!UserServiceForStudentManagement.isExistUser(username))
		{
			if(UserServiceForStudentManagement.register(user)) {
				if (level.equals("用户")) {
					request.getSession().setAttribute("user", user);//将用户对象放到session中
					//转发到user.jsp中
					request.getRequestDispatcher("user.jsp").forward(request, response);
				} else {
					request.getSession().setAttribute("admin", user);//将管理员对象放到session中
					//转发到user.jsp中
					request.getRequestDispatcher("admin.jsp").forward(request, response);
				}
			}
		}else {//失败
			request.setAttribute("info"," 错误:注册失败,请重新注册！");
			request.getRequestDispatcher("message.jsp").forward(request, response);
		}
	}

}
